<?php
class Editpassword extends CI_Controller {
	private $user_info;
	public function __construct() {
		parent::__construct ();
		// 登录校验
		$this->common->check_login_mobile ();
		// 通过user_model的get_user_info_id方法获取个人信息
		$this->user_info = $this->user->get_user_info_id ( $_SESSION ['mobile'] ['id'] );
	}
	
	/**
	 * 修改密码页面显示
	 */
	public function index() {
		$this->load->helper ( 'form' );
		
		$data ['left'] = $this->load->view ( 'mobile/left', '', TRUE );
		
		$data ['foot'] = $this->load->view ( 'mobile/foot', '', TRUE );
		
		$this->load->view ( 'mobile/editpwd', $data, FALSE );
	}
	
	/**
	 * 新密码提交
	 */
	public function info_post() {
		if (empty ( $_POST )) {
			exit ( $this->common->alert_msg_goback ( '参数错误' ) );
		}
		
		$old_pwd = md5 ( $_POST ['old_pwd'] );
		// 一级密码修改请求
		if ($_POST ['action'] == 'one') {
			if ($old_pwd != $this->user_info ['pwd']) {
				exit ( $this->common->alert_msg_goback ( '原密码输入错误' ) );
			} else {
				$this->load->model ( 'user_model' );
				$result = $this->user_model->update_pwd ( $this->user_info ['id'], md5 ( $_POST ['pwd'] ), 1 );
				
				if ($result) {
					exit ( $this->common->alert_msg_url ( '密码更新成功', site_url ( 'mobile/login' ) ) );
				} else {
					exit ( $this->common->alert_msg_goback ( '更新失败' ) );
				}
			}
		}
		// 二级密码修改请求
		if ($_POST ['action'] == 'two') {
			if ($old_pwd != $this->user_info ['spwd']) {
				exit ( $this->common->alert_msg_goback ( '原密码输入错误' ) );
			} else {
				$this->load->model ( 'user_model' );
				$result = $this->user_model->update_pwd ( $this->user_info ['id'], md5 ( $_POST ['spwd'] ), 2 );
				
				if ($result) {
					exit ( $this->common->alert_msg_url ( '密码更新成功', site_url ( 'mobile/editpassword' ) ) );
				} else {
					exit ( $this->common->alert_msg_goback ( '更新失败' ) );
				}
			}
		}
	}
}